package org.mzx.shortLink.adminn.remoteApi.Api;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.mzx.shortLink.adminn.remoteApi.dto.Request.TLinkCountDtoReq;
import org.mzx.shortLink.adminn.remoteApi.dto.Request.TLinkCreateDtoReq;
import org.mzx.shortLink.adminn.remoteApi.dto.Request.TLinkUpdateDtoReq;
import org.mzx.shortLink.adminn.remoteApi.dto.Response.TLinkCreateDtoResp;
import org.mzx.shortLink.adminn.remoteApi.dto.Response.TLinkPageDtoResp;
import org.mzx.shortLink.starter.convention.Result.Result;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 这里是走http请求，在项目内模拟http请求，远程调用我们work包中的接口
 * 后续可以微服务化改成feign+nacos调用
 */


@Component
public class LinkApi {

    /**
     * 创建短链接
     * @param requestParam
     * @return
     */
    public Result<TLinkCreateDtoResp> create(TLinkCreateDtoReq requestParam) {
        String result = HttpUtil.post("http://localhost:8001/api/short-link/v1/create", JSONObject.toJSONString(requestParam));
        return JSONObject.parseObject(result, new TypeReference<>() {
        });
    }

    /**
     * 模拟http请求分页查询
     * @param gid 分组id
     * @param current 目前页数
     * @param size  每页的大小
     * @return  分页
     */
    public Result<IPage<TLinkPageDtoResp>> getByPage(String gid, Long current, Long size) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("gid",gid);
        paramMap.put("current",current);
        paramMap.put("size",size);
        String result = HttpUtil.get("http://localhost:8001/api/short-link/v1/page", paramMap);
        return JSONObject.parseObject(result, new TypeReference<>() {
        });
    }


    /**
     * 查询某个分组下的短连接数量
     * @param requestParam
     * @return  一个Map
     */
    public Result<Map<String, Map<String, Object>>> count(List<TLinkCountDtoReq> requestParam) {

        String result = HttpUtil.post("http://localhost:8001/api/short-link/v1/count", JSONObject.toJSONString(requestParam));
        return JSONObject.parseObject(result, new TypeReference<>() {
        });
    }

    /**
     * 更新短链接
     * @param requestParam
     * @return
     */
    public Result<Void> update(@RequestBody TLinkUpdateDtoReq requestParam) {
        String result = HttpUtil.post("http://localhost:8001/api/short-link/v1/update", JSONObject.toJSONString(requestParam));
        return JSONObject.parseObject(result, new TypeReference<>() {
        });
    }
    public Result<String> getTitleByUrl(@RequestParam("url") String url) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("url",url);
        String result = HttpUtil.get("http://localhost:8001/api/short-link/v1/title",paramMap);
        return JSONObject.parseObject(result, new TypeReference<>() {
        });
    }
    public Result<Void> reDirect(String shortUrl, HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("shortUrl",shortUrl);
        paramMap.put("httpRequest",httpRequest);
        paramMap.put("httpResponse",httpResponse);
        String result = HttpUtil.get("http://localhost:8001",paramMap);
        return JSONObject.parseObject(result, new TypeReference<>() {
        });

    }
}
